/*
  东北大学ACTION码盘 采用ops_9定位系统 详细参��使用手冄1�7
	
   该部分负责收发码盘的原始数据后在taks了解析出相应的位置坐标后交给导航处理成期望的运动向量
*/


/* Includes ----------------------------------------------------------------- */
#include "tim.h"
#include "Action.h"
#include <string.h>
static osThreadId_t thread_alert;

uint8_t rxbuf[RS232_FRAME_LENGTH];

static void Ops10msTimerCallback(void *arg){
  (void)arg;
  osThreadFlagsSet(thread_alert,SIGNAL_OPSTIMER_REDY);
}

/* Private function  -------------------------------------------------------- */
static void ACTION_IdleCallback(void) {	
	osThreadFlagsSet(thread_alert,SIGNAL_ACTION_RAW_REDY);
}

/* Exported functions ------------------------------------------------------- */
int8_t ACTIONRECV_Init(Action_POS_t *pos){
	if(pos == NULL) return DEVICE_ERR_NULL;
    if((thread_alert = osThreadGetId()) == NULL ) return DEVICE_ERR_NULL;
  
   pos->action_timer_Id =
      osTimerNew(Ops10msTimerCallback, osTimerPeriodic,NULL,NULL);
	
	osTimerStart(pos->action_timer_Id,10);//此处ticks 理解为1个tick为1s 此处为每10ms触发一次回调函数   
	
   BSP_UART_RegisterCallback(BSP_UART_ACTION,BSP_UART_IDLE_LINE_CB,
                            ACTION_IdleCallback);
	
   return DEVICE_OK;
}

int8_t ACTION_StartReceiving() {
  if (HAL_UARTEx_ReceiveToIdle_DMA(BSP_UART_GetHandle(BSP_UART_ACTION),
                           (uint8_t *)rxbuf,
                           sizeof(rxbuf)) == HAL_OK)
    return DEVICE_OK;
  return DEVICE_ERR;
}

bool_t ACTION_WaitDmaCplt(void) {
  return(osThreadFlagsWait(SIGNAL_ACTION_RAW_REDY, osFlagsWaitAll,400) ==
          SIGNAL_ACTION_RAW_REDY);   
}


/* *
优化方向:考虑将利用定时器1ms计算出的速度与该函数做融各1�7

*/
int8_t ACTION_Parse(Action_POS_t *pos)
{
  if (pos == NULL) return DEVICE_ERR_NULL;
    if(rxbuf[0] ==0x11&&rxbuf[1]==0x04)
		{
//解析原始数据
		*((char *)(&(pos->pos_x)))   = rxbuf[5];                         
	  *((char *)(&(pos->pos_x))+1) = rxbuf[4];
		*((char *)(&(pos->pos_x))+2) = rxbuf[3];
		*((char *)(&(pos->pos_x))+3) = rxbuf[2];
		pos->pos_x = -pos->pos_x;//此处根据安装位置决定方向
//同上		
		*((char *)(&(pos->pos_y)))   = rxbuf[9];                         
	  *((char *)(&(pos->pos_y))+1) = rxbuf[8];
		*((char *)(&(pos->pos_y))+2) = rxbuf[7];
		*((char *)(&(pos->pos_y))+3) = rxbuf[6];
		pos->pos_y = -pos->pos_y;//ͬ
		}
		else return -1;
	return DEVICE_OK;
}

//该函数用来计算速度度(利用获取的位置来计算相应的速度)
//此处获取的是真实的位置(mm)后每10ms做一次微分处理
int8_t ACTION_DataRefresh(Action_POS_t *pos){
	if (pos == NULL) return DEVICE_ERR_NULL;
	
  if (osThreadFlagsGet() & SIGNAL_OPSTIMER_REDY){
	   osThreadFlagsClear(SIGNAL_OPSTIMER_REDY);
	   		//计算每毫秒的速度 该函数每10ms调用
		pos->pos_Vx = (pos->pos_x - pos->pos_lastX) / 10;
		pos->pos_Vy = (pos->pos_y - pos->pos_lastY) / 10;

		pos->pos_lastX = pos->pos_x;
		pos->pos_lastY = pos->pos_y;
  } 
	return DEVICE_OK;
}



int8_t Action_HandleOffline(Action_POS_t *pos) {
  if (pos == NULL) return DEVICE_ERR_NULL;

  (void)pos;
  memset(pos, 0, sizeof(*pos));
  return 0;
}

///* 将字符串拼接 */
//void Strcat(char str1[],char str2[],uint8_t num)
//{
//	int i=0,j=0;

//	while(str1[i]!='\0')
//	i++;
//	
//	for(j=0;j<num;j++)
//	{
//		str1[i++]=str2[j];
//	}
//}
///*                           该部分函数均使用来做码盘的重定位系统                                  */
///*���־��Ծ�ֹ ��ʮ�������� */
//void ACT_Calibration(void)
//{
//	HAL_UART_Transmit(&huart1, "ACTR", 4, 100);
//}

///*����*/
///*����ǰλ����Ϊ0*/
//void ACT_ZeroClear(void)
//{
//	HAL_UART_Transmit(&huart1, "ACT0", 4, 100);
//}
///*����XYλ��*/
//void ACT_UpdateXY(float pos_x,float pos_y)
//{
//char update_xy[12]="ACTD";
//	static union
//	{
//		float XY[2];
//		char data[8];				
//	}set;
//	set.XY[0] =pos_x;
//	set.XY[1] =pos_y;
//	Strcat(update_xy,set.data,8);
//	
//	HAL_UART_Transmit(&huart1,update_xy,sizeof(update_xy),100);
//}




